<?php

namespace Api\Controller;

/**
 * 文化日历
 *
 * Class EventController
 * @package Api\Controller
 */
class EventController extends PublicController
{
    private $model;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = M('Event');
    }

    /**
     * 获取文化日历分页数据
     */
    public function getList()
    {
        $perPage = !empty($_GET['per_page']) ? intval($_GET['per_page']) : 10;
        $curPage = !empty($_GET['page']) ? intval($_GET['page']) : 1;
        $date = !empty($_GET['date']) ? $_GET['date'] : null;
        $where = "is_del=0";
        if ($date) {
            $where .= " and (start_date <= '{$date}' and end_date >= '{$date}')";
        }
        $count = $this->model->where($where)->count();
        $totalPage = ceil($count / $perPage);
        $list = $this->model->where($where)->field('id,title,cover,start_date,end_date,address')->order('sort = 0, sort asc, id desc')->limit(($curPage - 1) * $perPage, $perPage)->select();
        foreach ($list as $k => $v) {
            $list[$k]['cover'] = __DATAURL__ . $v['cover'];
           /* $start_date = strtotime($v['start_date']);
            $end_date = strtotime($v['end_date']);
            $list[$k]['start_date'] = date("Y年m月y",$start_date)."日";
            $list[$k]['end_date'] = date("Y年m月y",$end_date)."日";*/
            $arr_start_date = explode("-",$v['start_date']);
            $list[$k]['start_date'] = $arr_start_date[0]."年".$arr_start_date[1]."月".$arr_start_date[2]."日";
            $arr_end_date = explode("-",$v['end_date']);
            $list[$k]['end_date'] = $arr_end_date[0]."年".$arr_end_date[1]."月".$arr_end_date[2]."日";
        }

        $this->json_result([
            'per_page'   => $perPage,
            'total'      => $count,
            'total_page' => $totalPage,
            'cur_page'   => $curPage,
            'list'       => $list,
        ]);
    }

    /**
     * 获取文化日历详情
     */
    public function detail()
    {
        $id = intval($_REQUEST['id']);
        $detail = $this->model->where(['id' => $id])->find();
        if (!$detail) {
            $this->json_error('没有找到相关信息');
        }

        $detail['content'] = str_replace(['../../../Data/', "/Data/"], ['/Data/', __DATAURL__], $detail['content']);
        $detail['cover'] = __DATAURL__ . $detail['cover'];
        $detail['apply'] = 0;

        // 当前用户是否已报名
        if ($userId = intval($this->get("id"))) {
            if ($apply = M('event_apply')->where(['event_id' => $id, 'user_id' => $userId])->find()) {
                if ($apply['apply_status']==2 || $apply['apply_status']==1) {
                    $detail['apply'] = 1;
                    $detail['apply_id'] = $apply['id'];
                } else {
                    $detail['apply'] = 2;
                    $detail['apply_id'] = $apply['id'];;
                }
            }
        }

        $this->json_result($detail);
    }

    /**
     * 活动预约申请
     */
    public function apply()
    {
        $userId = intval($this->get("id"));
        if (!$userId) {
            $this->json_error("请登录");
        }

        if (empty($_POST['event_id']) || empty($_POST['name']) || empty($_POST['mobile']) || empty($_POST['number']) || empty($_POST['form_id'])) {
            $this->json_error("参数错误");
        }

        $data = [
            'event_id'  => intval($_POST['event_id']),
            'user_id'   => $userId,
            'name'      => $_POST['name'],
            'mobile'    => $_POST['mobile'],
            'number'    => $_POST['number'],
            'form_id'   => $_POST['form_id'],
            'create_at' => date('Y-m-d H:i:s'),
        ];
        //判断改活动时候已经开放抢票
        $find_data = M("event")->where("id=".$_POST['event_id'])->find();
        if ( $find_data['status']==2 ) {
            $this->json_error("操作失败");
        }
        $where = "event_id=".$data['event_id']." AND user_id=".$data['user_id']." AND apply_status!=3";
        if (M('event_apply')->where($where  )->find()) {
            $this->json_error('请勿重复报名');
        }

        $result = M('event_apply')->add($data);
        if ($result) {
            $this->json_result([], '操作成功');
        } else {
            $this->json_error("操作失败");
        }
    }

    /**
     * 我的活动预约
     */
    public function myApply()
    {
        $userId = intval($this->get("id"));
        if (!$userId) {
            $this->json_error("请登录");
        }

        $perPage = !empty($_GET['per_page']) ? intval($_GET['per_page']) : 10;
        $curPage = !empty($_GET['page']) ? intval($_GET['page']) : 1;
        $where = "user_id=" . $userId;

        $count = M('event_apply')->where($where)->count();
        $totalPage = ceil($count / $perPage);
        $list = M('event_apply')->where($where)->join('lr_event on lr_event.id = lr_event_apply.event_id')
            ->field('lr_event_apply.*, lr_event.title, lr_event.cover,lr_event.address,lr_event.start_date,lr_event.end_date')->order('lr_event_apply.id desc')->limit(($curPage - 1) * $perPage, $perPage)->select();
        foreach ($list as $k => $v) {
            $list[$k]['cover'] = __DATAURL__ . $v['cover'];
        }

        $this->json_result([
            'per_page'   => $perPage,
            'total'      => $count,
            'total_page' => $totalPage,
            'cur_page'   => $curPage,
            'list'       => $list,
        ]);
    }

    /**
     * 活动报名详情
     */
    public function applyDetail()
    {
        $userId = intval($this->get("id"));
        if (!$userId) {
            $this->json_error("请登录");
        }

        $applyId = intval($_GET['id']);
        if (!$applyId) {
            $this->json_error("参数错误");
        }

        $apply = M('event_apply')->where(['user_id' => $userId, 'id' => $applyId])->find();
        if (empty($apply)) {
            $this->json_error("数据错误");
        }

        !empty($apply['photo']) && $apply['photo'] = __DATAURL__ . $apply['photo'];

        $apply['event'] = M('event')->where(['id' => $apply['event_id']])->find();
        !empty($apply['event']['cover']) && $apply['event']['cover'] = __DATAURL__ . $apply['event']['cover'];

        $this->json_result($apply);
    }
}